<!--
 * @Description:
 * @Author: william
 * @Date: 2024-04-15 10:36:50
 * @LastEditors: william
 * @LastEditTime: 2025-07-14 10:45:11
-->
<template>
  <!-- v-if="hasDefaultSlot" -->
  <el-input
    ref="refInput"
    v-model="modelValue"
    :placeholder="getPlaceholder()"
    :maxlength="maxlength"
    :clearable="!verifyType && clearable"
    :="$attrs"
    @input="handleInput"
  >
    <template #prefix v-if="$slots.prefix">
      <slot name="prefix" />
    </template>
    <template #suffix v-if="$slots.suffix">
      <slot name="suffix" />
    </template>
    <template #prepend v-if="$slots.prepend">
      <slot name="prepend" />
    </template>
    <template #append v-if="$slots.append">
      <slot name="append" />
    </template>
  </el-input>
  <!-- <el-input
    v-else
    ref="refInput"
    v-model="modelValue"
    :placeholder="getPlaceholder()"
    :maxlength="maxlength"
    :clearable="!verifyType && clearable"
    :="$attrs"
    @input="handleInput"
  /> -->
</template>

<script setup>
const props = defineProps({
  verifyType: {
    type: String,
    default: '' // nzint-不以0开头的整数/int-整数/float-浮点数/alnum-字母或数字[a-zA-Z0-9]/idcard-身份证号/alnuml-字母或数字或中划线[a-zA-Z0-9\-]
  },
  maxlength: {
    type: Number,
    default: 128
  },
  clearable: {
    type: Boolean,
    default: true
  },
  placeholder: {
    type: String,
    default: ''
  },
  label: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['input'])
const modelValue = defineModel({
  type: String,
  default: ''
})
const slots = useSlots()
const refInput = ref(null)
// const hasDefaultSlot = computed(() => !!slots.prefix || !!slots.suffix)

const getPlaceholder = _ => {
  return (
    props.placeholder ||
    g_rules.zhAndEnShowMsg(g_t('public.pleaseInput'), props.label)
  )
}

// 对输入的字符进行过滤
const handleInput = value => {
  const verifyMap = {
    nzint: /^(0+)|[^1-9\^\d]+/g,
    int: /[^\d]+/g,
    float: /[^\d.]+/g,
    alnum: /[^\w]+/g,
    alnuml: /[^\w^\-]+/g,
    idcard: /[^\d^A-Z]+/g,
    cletter: /[^A-Z]+/g
  }
  modelValue.value = value.replace(verifyMap[props.verifyType] || '', '')
  emit('input', modelValue.value)
}

defineExpose({
  refInput
})
</script>
